package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.Ip;

public class IpDAO extends GenericDAO<Ip> {
	
	@Override
	public Long save(Ip ip) throws HibernateException {
		Long ipId = null;
		Session session = getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			ipId = (Long) session.save(ip);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		return ipId;
	}

	@Override
	public void saveOrUpdate(Ip ip) throws HibernateException {
		Session session = getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(ip);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(get(id));
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(Ip ip) throws HibernateException {
		Session session = getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.delete(ip);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@Override
	public Ip get(long id) throws HibernateException {
		Ip ip = null;
		Session session = getSessionFactory().openSession();		
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			ip = (Ip) session.get(Ip.class, id);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		return ip;
	}
	
	@SuppressWarnings("unchecked")
	public List<Ip> getIpsByArticleId(long articleId) {
		Session session = getSessionFactory().openSession();
		List<Ip> ips = (List<Ip>) session.createCriteria(Ip.class)
				.add(Restrictions.eq("articleId", articleId))
				.list();
		return ips;
	}
	
	public boolean isNewIpAddress(long articleId, String ipAddress) {
		
		StringBuilder hqlQuery = new StringBuilder();
		hqlQuery.append("select count(ip) from Ip ip ");
		hqlQuery.append("where ip.articleId = :articleId ");
		hqlQuery.append("and ip.ipAddress = :ipAddress ");		 
				
		long count = 0;
		Session session = getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery(hqlQuery.toString());
			query.setParameter("articleId", articleId);
			query.setParameter("ipAddress", ipAddress);
			count = (Long)query.uniqueResult();
			tx.commit();			
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		return (count == 0 ? true : false);
	}  

}
